<!-- 上级组织 -->
<template>
  <div class="company-box" :style="{ marginRight: isMargin ? '15px' : '' }">
    <Select
      v-model="companyId"
      placeholder="上级组织"
      class="company-select"
      clearable
      @on-change="onChange"
    >
      <Option v-for="item in listData" :key="item.value" :value="item.value">{{
        item.itemName
      }}</Option>
    </Select>
  </div>
</template>

<script type="text/javascript">
import {
  getCompanyList,
  GetDict, // 获取证件类型
} from "@/api/common";

export default {
  name: "btn-text",
  props: {
    isMargin: {
      type: Boolean,
      default: false,
    },
    type: {
      type: String,
      default: "1",
    },
    companyChangeId: {
      type: String,
      default: "",
    },
  },
  watch: {
    companyChangeId: function (val) {
      this.companyId = val;
    },
  },
  data() {
    return {
      companyId: "",
      listData: [],
    };
  },
  methods: {
    async getCompanyListData() {
      let data = await getCompanyList();
      data.forEach((element) => {
        element.value = element.companyId;
        element.name = element.companyName;
        element.itemName = element.companyName;
      });
      this.listData = data;
      console.log(data, "getCompanyList");
    },
    async getDictData(key, name) {
      let data = await GetDict({
        parentId: key,
      });
      console.log(data, "data");
      data.forEach((element) => {
        element.value = element.name;
        element.itemName = element.name;
        element.name = element.dictId;
      });
      this[name] = data;
    },
    onChange(ev) {
      this.$emit("update:companyId", ev);
    },
  },
  mounted() {
    let { type } = this.$props;
    if (type == "1") {
      this.getCompanyListData();
    }
    if (type == "2") {
      this.getDictData("COMPANY_CARD_TYPE", "listData"); // 获取证件类型
    }
  },
};
</script>

<style lang="less" scoped>
.company-box {
  width: 100%;
}
</style>
